<?php
//公共函数文件

//时间戳转换为时间 输出格式为 Y-m-d
function rtdate($val)
{
    if ($val == "" || $val == 0) {
        $strtime = "";
    } else {
        $strtime = date("Y-m-d", $val);
    }
    return $strtime;
}

//时间戳转换为时间 输出格式为 Y-m-d H:i:s
function rttime($field)
{
    if ($field == "" || $field == 0) {
        $strtime = "";
    } else {
        $strtime = date("Y-m-d H:i:s", $field);
    }
    return $strtime;
}

//将年 月 日时间转化为时间戳
function timestamp($val)
{
    if ($val == "" || $val == 0) {
        $time = 0;
    } else {
        $time = strtotime($val);
    }
    return $time;
}

//生成随机字符串
function makeCardPassword(): string
{
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $rand = $code[rand(0, 25)]
        . strtoupper(dechex(date('m')))
        . date('d') . substr(time(), -5)
        . substr(microtime(), 2, 5)
        . sprintf('%02d', rand(0, 99));
    for (
        $a = md5($rand, true),
        $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
        $d = '',
        $f = 0;
        $f < 8;
        $g = ord($a[$f]),
        $d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F],
        $f++
    ) ;
    return $d;
}

//生成加密字符串
function md5EncryptionStr($str, $salt, $num)
{
    return md5(md5($str) . $salt . $num);
}

/**
 * mysql文件内容
 * @param string $FilePath 存储路径
 * @param int $Order 排序：0升序，1倒序
 * @return array 文件列表数组
 */
function myScandir(string $FilePath = './', int $Order = 0): array
{
    $FilePath = opendir($FilePath);
    while (false !== ($filename = readdir($FilePath))) {
        $FileAndFolderAyy[] = $filename;
    }
    $Order == 0 ? sort($FileAndFolderAyy) : rsort($FileAndFolderAyy);
    return $FileAndFolderAyy;
}